﻿Imports DotNetNuke.Security.Roles
Imports DotNetNuke.Entities.Users

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers.Social

    Public Class SocialComponentsRewriter
        Inherits SimpleAPI.SimpleRewriterProvider
        Implements IIsActive


        Protected Overrides Function GetRewritingGroups() As System.Collections.Generic.List(Of SimpleAPI.SimpleGroupParamsRewrite)
            Dim results As New List(Of SimpleAPI.SimpleGroupParamsRewrite)
            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("SocialGroup", "Group name") _
                        .AddNonExclusiveParameter("groupid") _
                        .SetRewritingDelegate(AddressOf GetGroupName) _
                        .Create())

            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("UserLogin", "Login of the user") _
                       .AddNonExclusiveParameter("userid") _
                       .SetRewritingDelegate(AddressOf GetUserLogin) _
                       .Create())

            Return results
        End Function

        Private Function GetGroupName(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Dim RC As New RoleController()
            Dim ri As RoleInfo = RC.GetRole(CurrentGroup.SingleParameterAsInteger(params), PortalId)

            Return ri.RoleName
        End Function

        Private Function GetUserLogin(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Dim UC As New UserController()
            Dim ui As UserInfo = UC.GetUser(PortalId, CurrentGroup.SingleParameterAsInteger(params))

            If ui IsNot Nothing Then
                Return ui.Username
            End If

            Return String.Empty
        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return NukeHelper.DnnVersion.Major > 6 OrElse (NukeHelper.DnnVersion.Major = 6 AndAlso NukeHelper.DnnVersion.Minor >= 2)
            End Get
        End Property
    End Class

End Namespace